layui.use(['element', 'table', 'jquery', 'laydate', 'form'], function () {
    var element = layui.element;
    var table = layui.table;
    var laydate = layui.laydate;
    var form = layui.form;
    let code = JSON.parse(sessionStorage.getItem("userInfo")).code;
    $(function () {
        tableRender();
    })

    function tableRender() {
        table.render({
            elem: '#words_table'
            // , height: 312
            ,
            url: LOCALHOSTURL + '/words/words/getSelfWords' //数据接口
            ,
            method: "get",
            where: {
                "code": code
            },
            xhrFields: {
                withCredentials: true //默认情况下，标准的跨域请求是不会发送cookie的
            },
            headers: {
                "Authorization": $.cookie("token")
            },
            crossDomain: true,
            response: {
                statusName: 'code' //规定数据状态的字段名称，默认：code
                ,
                statusCode: 200 //规定成功的状态码，默认：0
            },
            page: true //开启分页
            ,
            title: "单词表",
            toolbar: '#toolbar',
            defaultToolbar: ['filter', 'exports', 'print'],
            cols: [
                [ //表头
                    {
                        type: 'checkbox',
                        fixed: 'left'
                    }, {
                    field: 'id',
                    title: 'ID',
                    sort: true
                }, {
                    field: 'english',
                    title: '单词',
                    templet: function (res) {
                        if (res.english == null || res.english == '')
                            return '暂无';
                        else
                            return res.english;
                    }
                }, {
                    field: 'chinese',
                    title: '翻译',
                    templet: function (res) {
                        if (res.chinese == null || res.chinese == '')
                            return '暂无';
                        else
                            return res.chinese;
                    }
                }, {
                    field: 'author',
                    title: '作者',
                    templet: function (res) {
                        if (res.author != null && res.author != "")
                            return res.author;
                        else
                            return '暂无';
                    }
                }, {
                    field: 'createTime',
                    title: '发布时间',
                    templet: function (res) {
                        return res.createTime;
                    }
                }, {
                    field: 'likesCount',
                    title: '点赞数',
                    templet: function (res) {
                        if (res.likesCount == null)
                            return 0;
                        else
                            return res.likesCount;
                    }
                }, {
                    fixed: 'right',
                    title: '操作',
                    toolbar: '#barDemo',
                    width: 150
                }
                ]
            ],
            done: function (res) {
                console.log(res);
            }
        });
    }

    table.on("tool(words_filter)", function (obj) {
        let data = obj.data;
        switch (obj.event) {
            case 'edit':
                layer.open({
                    type: 2,
                    title: "修改单词",
                    area: ['100%', '100%'],
                    content: ["editWords.html"],
                    shadeClose: true, //点击旁边地方自动关闭
                    //动画
                    anim: 0,
                    success: function (layero, index) {
                        var body = layer.getChildFrame('body', index);
                        // var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象
                        var iframeWin = layero.find('iframe')[0].contentWindow;
                        console.log(iframeWin);
                        iframeWin.editWord(JSON.stringify(data),index);
                    }
                });
                break;
            case 'del':
                layer.confirm("真的删除单词" + data.english + "?", function (index) {
                    // obj.del();
                    let result = myAjax("/words/words/deleteWords", "get", {id: data.id});
                    alert(result.message);
                    tableRender();
                    layer.close(index);
                });
                break;
        }
    })
});